package org.leaf.green.b2b

import org.leaf.green.admin.RecordType

import org.leaf.green.shiro.User

class StockIn {//入库单

    def stockInService
    String serialNumber
    RecordType recordType//记录类型
    Purchase purchase//采购单或者采摘计划
    Dealer dealer//供应商
    Date date//单据日期
    Staff maker//制单人
    Boolean approved//状态

    Date dateCreated
    Date lastUpdated

    static hasMany = [details : StockInDetail]

    static constraints = {
        serialNumber(nullable:false, blank:false, unique : true, maxSize : 11)
        recordType(nullable:false)
        purchase(nullable:false)
        dealer(nullable:true)
        date(nullable:false)
        maker(nullable:false)
        approved(nullable:false)
    }

    def beforeUpdate(){
        if(this.approved == true && this.getPersistentValue('approved') == false){
            log.info("[domain=StockIn, method=beforeUpdate] update inventory")
            //def result = false
            Inventory.withNewSession{
                stockInService.updateInventory(this)
            }
        }
    }

    static mapping = {
        table 'B2B_STOCKIN'
        details cascade:"all-delete-orphan", sort:"serialNumber", batchSize: 100
    }
}
